<template>
	<view class="goods-list">
		<view v-for="(goods,index) in goods_list" :key="index" @click="gotoGoodsDetail(goods)">
			<my-goods :item="goods"></my-goods>
		</view>
		
		<view class="bottom-view" v-if="bottomFlag">我是有底线的~~~</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//请求参数对象
				queryObj:{
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goods_list: [],
				total: 0,
				bottomFlag:false,
				//是否正在请求数据
				isloading: false
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			console.log(this.queryObj)
			this.getListData()
		},
		onPullDownRefresh() {
			console.log('下拉刷新了,清空数据')
			//重置关键数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isloading = false
			this.goods_list = []
			
			this.getListData(()=>{uni.stopPullDownRefresh()})
		},
		onReachBottom() {
			this.queryObj.pagenum += 1
			console.log(this.goods_list.length,this.total)
			if(this.goods_list.length === this.total){
				console.log('到底了没有下一页了')
				this.bottomFlag = true
			}else {
				if (this.isloading) return
				this.getListData()
			}
		},
		methods:{
			async getListData(callBack) {
				//打开节流阀
				this.isloading = true
				const {data:res} =	await uni.$http.get('/goods/search',this.queryObj)
				console.log('res',res)
				//关闭节流阀
				this.isloading = false
				if(res.meta.status !== 200) {
					return uni.$showErrMsg()
				}
				// callBack & callBack()
				this.goods_list = [...this.goods_list,...res.message.goods]
				this.total = res.message.total
			},
			gotoGoodsDetail(item){
				console.log(item.goods_id)
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
				})
			}
		}
	}
</script>

<style lang="scss">
	.goods-list {
		
		.bottom-view{
			height: 60px;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>
